#include <stdio.h>
#include <string.h>
#include <math.h>

#define LETTERS "ABCDEFG" "HIJKLMN" "OPQRST" "UVWXYZ"

//prototype
void fourth(void);
void fifth(void);
void twelfth(void);
void thirteenth(void);
void fourteenth(void);
void sixteenth(void);
void seventeenth(void);
void eighteenth(void);

int main(void)
{
    printf("the question 4.\n");
    fourth();

    putchar('\n');
    printf("the question 5.\n");
    fifth();

    putchar('\n');
    printf("the question 12.\n");
    twelfth();

    putchar('\n');
    printf("the question 13.\n");
    thirteenth();

    putchar('\n');
    printf("the question 14.\n");
    fourteenth();

    putchar('\n');
    printf("the question 16.\n");
    sixteenth();


    putchar('\n');
    printf("the question 17.\n");
    seventeenth();


    putchar('\n');
    printf("the question 18.\n");
    eighteenth();
}


/**
 * printf:
 * A
 * BC
 * DEF
 * GHIJ
 * KLMNO
 * ...
 */
void fourth(void)
{
    char ch = 0;
    int i, j;
    for (i=0; i<6; ++i) {
        for (j=0; j<=i; ++j)
            printf("%c", LETTERS[ch++]);
        putchar('\n');
    }

}


/**
 * print:
 *     A
 *    ABA
 *   ABCBA
 *  ABCDCBA
 * ABCDEDCBA
 */
void fifth(void)
{
    char maxch;
    int max,i,j,k,l;

    int llen = strlen(LETTERS);
    do {

        printf("\nplease input a Maxium letter: ");
        scanf("%c", &maxch);

        for (max=0; max<llen; ++max) {
            if (LETTERS[max] == maxch)
                break;
        }

    } while (max == llen);

    for (i=0; i<=max; ++i) {
        for (j=0; j<max-i; ++j)
            putchar(' ');

        for (j=0; j<=i; ++j) {
            putchar(LETTERS[j]);

        }

        for (k=i-1; k>=0; --k)
            putchar(LETTERS[k]);

        putchar('\n');
    }
}

void twelfth_cal(int n, double *a, double *b)
{
    int i;
    double res1 = 0.0, res2 = 0.0;

    for (i=1; i<=n; ++i) {
        res1 += 1.0 / i;
        res2 += 1.0 / (i % 2 == 0 ? -(i) : i);
    }

    *a = res1;
    *b = res2;
}

/**
 * 1.0 + 1.0/2 + 1.0/3 + 1.0/4 + ... 1.0/n
 * 1.0 - 1.0/2 + 1.0/3 - 1.0/4 + ... 1.0/n
 */
void twelfth(void)
{
    int times;
    int i;
    double res1, res2;


    times =0;

    printf("please input a number of times: ");
    while (scanf("%d", &times) == 1) {
        twelfth_cal(times, &res1, &res2);

        if (times <= 0) {
            printf("end!\n");
            return;
        }

        printf("first seq result is: %.12lf\n", res1);
        printf("second seq result is: %.12lf\n", res2);

        printf("please input a number of times: ");
    }

    printf("You should input a digit!\n");
}

void thirteenth(void)
{
    int a[8];
    int i;

    for (i=0; i<8; ++i) {
        a[i] = pow(2, i);
    }

    i=0;
    do {
        printf("a[%d] = %d\n", i, a[i]);
    } while (++i < 8);
}

void fourteenth(void)
{
    double d[8];
    int i;

    for (i=0; i<8; ++i) {
        scanf("%lf", &d[i]);
    }

    double e[8];
    for (i=0; i<8; ++i) {
        e[i] = d[i] + (i==0 ? 0.0 : e[i-1]);
    }

    printf("d: ");
    for (i=0; i<8; ++i) {
        printf("%8.2lf ", d[i]);
    }

    printf("\ne: ");
    for (i=0; i<8; ++i) {
        printf("%8.2lf ", e[i]);
    }
    putchar('\n');
}

void sixteenth(void)
{
    const double orig = 100;
    double dap, dei;

    int n;

    dap = dei = orig;

    n=0;
    while (dei <= dap) {
        dap += orig * 0.1;
        dei += dei * 0.05;
        ++n;

        printf("%2d year later, dei:%.2lf dap:%2lf\n",n, dei, dap);
    }

    printf("%2d year later, dei(%.2lf) surpassed dap(%.2lf)\n",n , dei, dap);
}

void seventeenth(void)
{
    double mon = 1000000.0;
    const double rate = 0.08;
    const double pop = 100000.0;

    int n =0;

    while (1) {
        n++;
        mon = mon + mon * rate;

        printf("the %02d year end, ", n);
        if (mon < pop) {
            printf("chuckie get out $:%.2lf, and no money in the bank!", mon);
            break;
        }

        mon -= pop;
        printf("chuckie get out $%.2lf, and $%.2lf in the bank!\n", pop, mon);
    }
    putchar('\n');
}

void eighteenth(void)
{
    const int dunbar = 150;
    int rab = 5;

    int n=0;
    while (rab <= dunbar) {
        ++n;
        rab = (rab - n) * 2;
        printf("the %d week, rabnud had %d friends.\n", n, rab);
    }

    printf("when %d week, rabnud's friend over than dunbar's friend(%d)\n", n, dunbar);
}
